package face.pay.web.company.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import face.pay.common.user.service.UserRegisterBiz;
import face.pay.constant.RoleCode;
import face.pay.constant.UserType;
import face.pay.dao.CompanyInfoDao;
import face.pay.entity.CompanyInfo;
import face.pay.entity.OemInfo;
import face.pay.entity.UserInfo;
import face.pay.security.util.SecurityUserUtil;
import face.pay.util.ResultMessage;
import face.pay.util.page.PageData;
import face.pay.util.page.PageParam;
import face.pay.web.company.service.ICompanyInfoService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @Classname CompanyInfoServiceImpl
 * @Description JJL
 * @Date 2019/11/5 14:19
 */
@Service
@Slf4j
@Transactional(rollbackFor = Exception.class)
public class CompanyInfoServiceImpl implements ICompanyInfoService {

    @Autowired
    private CompanyInfoDao companyInfoDao;

    @Autowired
    UserRegisterBiz userRegisterBiz;

    /**
     * 查询全部第三方公司，可根据名称查询
     *
     * @param name
     * @return
     */
    @Override
    public PageData queryPage(String name, PageParam pageParam) {
        PageHelper.startPage(pageParam.getPageNo(),pageParam.getPageSize());

        List<CompanyInfo> oemList = this.companyInfoDao.queryList(name);

        PageInfo<CompanyInfo> pageInfo = new PageInfo <>(oemList);

        PageData<CompanyInfo> pageData = new PageData<>(pageInfo.getTotal(),oemList);

        return pageData;
    }

    /**
     * 根据id判断是新增还是修改
     *
     * @param companyInfo
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResultMessage editCompany(CompanyInfo companyInfo) {
        Integer oemId = companyInfo.getId();
        if(oemId==null){
            return  this.addCompany(companyInfo);
        }else {
            return  this.modifyCompany(companyInfo);
        }
    }

    public ResultMessage addCompany(CompanyInfo companyInfo) {
        UserInfo loginUserInfo =  SecurityUserUtil.getCurrentLoginUser();

        Integer loginUserId = loginUserInfo.getId();

        log.info("操作用户："+loginUserInfo.getUserName()+ "新增company");

        companyInfo.setCreatedBy(loginUserId);

        companyInfoDao.insert(companyInfo);

        UserInfo userInfo = new UserInfo();
        userInfo.setCompanyId(companyInfo.getId());
        userInfo.setOemId(loginUserInfo.getOemId());
        userInfo.setRecommendId(loginUserId);
        userInfo.setUserName(companyInfo.getUserName());
        userInfo.setMobile(companyInfo.getMobile());
        userInfo.setType(UserType.COMPANY);

        this.userRegisterBiz.registUserWithRole(RoleCode.ROLE_COMPANY_MANAGER,userInfo);

        return ResultMessage.createSuccess();
    }

    public ResultMessage modifyCompany(CompanyInfo companyInfo) {
        UserInfo loginUserInfo =  SecurityUserUtil.getCurrentLoginUser();

        Integer loginUserId = loginUserInfo.getId();

        log.info("操作用户："+loginUserInfo.getUserName()+ "修改company信息");

        companyInfo.setUpdatedBy(loginUserId);

        companyInfoDao.update(companyInfo);

        UserInfo userInfo = new UserInfo();
        userInfo.setId(companyInfo.getUserId());
        userInfo.setUserName(companyInfo.getUserName());
        userInfo.setMobile(companyInfo.getMobile());

        this.userRegisterBiz.updateUser(userInfo);

        return ResultMessage.createSuccess();
    }
}
